package com.osdp.base.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Maps;
import com.osdp.common.http.Result;
import com.osdp.common.util.BeansUtil;
import com.osdp.common.util.ConvertUtil;
import com.osdp.base.entity.SysDictItem;
import com.osdp.base.service.ISysDictItemService;
import com.osdp.base.vo.SysDictItemVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

@Api(tags = "数据字典信息")
@RestController
@RequestMapping("/base/dictitem")
public class SysDictItemController {
    @Autowired
    private ISysDictItemService sysDictItemService;

    @PostMapping("/list/page")
    public Result<IPage<SysDictItemVo>> pageList(@RequestBody SysDictItemVo params
            //@RequestParam(name = "pageNo", defaultValue = "1") int pageNo,
//            @RequestParam(name = "pageSize", defaultValue = "10") int pageSize
    ){
        SysDictItem sysDictItem = ConvertUtil.sourceToTarget(params, SysDictItem.class);
        IPage<SysDictItemVo> result = sysDictItemService.pageList(sysDictItem,new Page<>(params.getPageNo(), params.getPageSize())).convert(track -> BeansUtil.copy(track, new SysDictItemVo()));
        return Result.success(result);
    }

    @PostMapping("/insert")
    public boolean insert(@RequestBody SysDictItemVo params){
        SysDictItem sysDictItem = ConvertUtil.sourceToTarget(params, SysDictItem.class);
        return sysDictItemService.insertDictItem(sysDictItem);
    }

    @PutMapping("/update")
    public boolean update(@RequestBody SysDictItemVo params){
        SysDictItem sysDictItem = ConvertUtil.sourceToTarget(params, SysDictItem.class);
        return sysDictItemService.updateDictItem(sysDictItem);
    }

    @DeleteMapping("/delete")
    public boolean delete(@RequestParam(name = "id", required = true) String id ){
        return sysDictItemService.deleteDictItem(id);
    }

    @GetMapping(value = "/list/byCode/{dictItemCode}")
    public List<SysDictItemVo> getListByCode(@PathVariable(value = "dictItemCode") String dictItemCode ){
        List<SysDictItem> itemList =  sysDictItemService.getDictListByCode(dictItemCode);
        List<SysDictItemVo> dictVoList = ConvertUtil.sourceToTarget( itemList, SysDictItemVo.class);
        return dictVoList;
    }

    @RequestMapping(value = "/list/all",  method = RequestMethod.POST)
    public Result<List<SysDictItemVo>> queryAllList(@RequestBody SysDictItemVo params){
        SysDictItem dictItem = ConvertUtil.sourceToTarget(params, SysDictItem.class);
//        List<SysDictItemVo> resultList = Arrays.asList();
        List<SysDictItemVo> dictList = sysDictItemService.queryAllList(dictItem).stream().map(e -> {
            SysDictItemVo vo = ConvertUtil.sourceToTarget(e, SysDictItemVo.class);
            return vo;
        }).collect(Collectors.toList());
        return Result.success(dictList);
    }

    @ApiOperation(value = "根据多个ode获取值集项列表")
    @PostMapping("/list/codes")
    public Result<Map<String,List<SysDictItemVo>>> queryMapByItemCodes(@RequestBody List<String> params ){
        List<SysDictItem> itemList = sysDictItemService.queryAllList(new SysDictItem());
        Map<String,List<SysDictItemVo>> dictItemlAllMap = getDictItemListMap(itemList);
        Map<String,List<SysDictItemVo>> dictItemlMap = Maps.newHashMap();
        params.stream().forEach(e ->{
            if(dictItemlAllMap.containsKey(e)){
                dictItemlMap.put(e,dictItemlAllMap.get(e));
            }
        });
        return Result.success(dictItemlMap);
    }

    private Map<String, SysDictItemVo> getDictItemtMap(List<SysDictItem> itemList) {
        Map<String, SysDictItemVo>  map = itemList.stream().map(e -> {
            SysDictItemVo vo = ConvertUtil.sourceToTarget(e, SysDictItemVo.class);
            return vo;
        }).collect(Collectors.toMap(SysDictItemVo:: getDictItemCode, Function.identity(),(key1, key2) -> key2));
        return map;
    }

    /**
     * 按dictItemCode字段分组
     * @param itemList
     * @return
     */
    private Map<String,List<SysDictItemVo>> getDictItemListMap(List<SysDictItem> itemList){
        Map<String, List<SysDictItemVo>> resultMap = itemList.stream().map(e -> {
            SysDictItemVo vo = ConvertUtil.sourceToTarget(e, SysDictItemVo.class);
            return vo;
        }).collect(Collectors.groupingBy(SysDictItemVo:: getDictItemCode));
        return resultMap;
    }

}
